package test.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class GetValue {
	
	/**
	 * 身份证号生成
	 * @return string
	 */
	public static String randomIdcard()
	{
		String str = ""; //身份证号码
		String [] a = AreaCode.code.split(",");
		String w = "7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2";  //17位校验码
		String arr[] = w.split(",");
		int i = randomNumber(0,a.length-1);
		str = a[i]+"19"+randomNumber(60,90);   //随机年
		i = randomNumber(1,9);  //随机月
		str += "0" + i;
		i = randomNumber(10,28);  //随机日
		str += i;
		i = randomNumber(100,999);  //随机三位数
		str += i;
		int temp = 0;
		for(i=0;i<17;i++)
		{
			temp = temp + Integer.valueOf(str.substring(i, i+1))*Integer.valueOf(arr[i]);
			
		}

		int t = temp%11;   //校验码

		switch(t) {
			case 0: str += '1'; break;
			case 1: str += '0'; break;
			case 2: str += 'x'; break;
			case 3: str += '9'; break;
			case 4: str += '8'; break;
			case 5: str += '7'; break;
			case 6: str += '6'; break;
			case 7: str += '5'; break;
			case 8: str += '4'; break;
			case 9: str += '3'; break;
			case 10: str += '2'; break;
		}

		return str;
	}
	
	/**
	 * 银行卡号生成
	 */
	public static String randomBankCard()
	{
		String str = "622700";  //初始化银行卡号
		int tmp = 0;
		int tmp2;
		int i;
		for (i = 6; i<18; i++)
		{
			str += randomNumber(0,9);
		}

		for(i = 17; i >= 0; i--)
		{
			if((i+1)%2 == 0)
			{
				tmp2 = Integer.valueOf(str.substring(i,i+1)) * 2;
				if(tmp2 > 9)
				{
					tmp = tmp + tmp2/10 + tmp2%10;
				}
				else
				{
					tmp = tmp + tmp2;
				}
			}
			else
			{
				tmp = tmp + Integer.valueOf(str.substring(i,i+1));
			}
		}
		if(tmp%10 == 0)
		{
			str += '0';
		}
		else
		{
			tmp = 10 - tmp%10;
			str += tmp;
		}
		return str;
	}
	
	/**
	 * 手机号生成
	 */
	public static String randomMobileNumber()
	{
		String str = "1";  //初始化手机号
		String arr[] = "3,5,4,8".split(",");  //手机号码第二位
		int count = arr.length;
		int i = randomNumber(0,count-1);
		str += arr[i];
		for(i=1; i<10; i++)
		{
			str += randomNumber(0, 9);
		}
		return str;
	}
	
	/**
	 * 生成电子邮箱
	 * @return
	 */
	public static String randomEmail() {
		String [] a = new String[] {"a","b","c","d","e","f","g","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
		String [] b = new String[] {"sohu","163","sina","126","qq"};
		String str = "";
		int counta = a.length;
		int countb = b.length;
		int count = randomNumber(5,10);
		int temp;
		for(int i=0;i<count;i++) {
			temp = randomNumber(0,counta-1);
			str += a[temp];
		}
		str += "@";
		temp = randomNumber(0,countb-1);
		str += b[temp]+".com";
		return str;
	}


	/**
	 * 生成街道地址
	 * @return
	 */
	public static String randomAddress(){
		String [] arrMing = new String[]{"伟","刚","勇","毅","俊","峰","强","军","平","保","东","文","辉","力","明","永","健","世","广","志","义","兴","良","海","山","仁","波","宁","贵","福","生","龙","元","全","国","胜","学","祥","才","发","武","新","利","清","飞","彬","富","顺","信","子","杰","涛","昌","成","康","星","光","天","达","安","岩","中","茂","进","林","有","坚","和","彪","博","诚","先","敬","震","振","壮","会","思","群","豪","心","邦","承","乐","绍","功","松","善","厚","庆","磊","民","友","裕","河","哲","江","超","浩","亮","政","谦","亨","奇","固","之","轮","翰","朗","伯","宏","言","若","鸣","朋","斌","梁","栋","维","启","克","伦","翔","旭","鹏","泽","晨","辰","士","以","建","家","致","树","炎","德","行","时","泰","盛","雄","琛","钧","冠","策","腾","楠","榕","风","航","弘","秀","娟","英","华","慧","巧","美","娜","静","淑","惠","珠","翠","雅","芝","玉","萍","红","娥","玲","芬","芳","燕","彩","春","菊","兰","凤","洁","梅","琳","素","云","莲","真","环","雪","荣","爱","妹","霞","香","月","莺","媛","艳","瑞","凡","佳","嘉","琼","勤","珍","贞","莉","桂","娣","叶","璧","璐","娅","琦","晶","妍","茜","秋","珊","莎","锦","黛","青","倩","婷","姣","婉","娴","瑾","颖","露","瑶","怡","婵","雁","蓓","纨","仪","荷","丹","蓉","眉","君","琴","蕊","薇","菁","梦","岚","苑","婕","馨","瑗","琰","韵","融","园","艺","咏","卿","聪","澜","纯","毓","悦","昭","冰","爽","琬","茗","羽","希","欣","飘","育","滢","馥","筠","柔","竹","霭","凝","晓","欢","霄","枫","芸","菲","寒","伊","亚","宜","可","姬","舒","影","荔","枝","丽","阳","妮","宝","贝","初","程","梵","罡","恒","鸿","桦","骅","剑","娇","纪","宽","苛","灵","玛","媚","琪","晴","容","睿","烁","堂","唯","威","韦","雯","苇","萱","阅","彦","宇","雨","洋","忠","宗","曼","紫","逸","贤","蝶","菡","绿","蓝","儿","翠","烟"};

		int countb = arrMing.length;

		String str = "北京市海淀区";
		str += arrMing[randomNumber(0,countb - 1)];
		for(int i=0;i<2;i++) {
			str += arrMing[randomNumber(0,countb - 1)];
		}

		return str+"街道"+randomNumber(1,9)+"-"+randomNumber(100,999)+"-"+randomNumber(0,9)+"号";
	}

	/**
	 * 生成姓名
	 * @return
	 */
	public static String randomName(){
		String arrXing[] = new String[] {"赵","钱","孙","李","周","吴","郑","王","冯","陈","褚","卫","蒋","沈","韩","杨","朱","秦","尤","许","何","吕","施","张","孔","曹","严","华","金","魏","陶","姜","戚","谢","邹","喻","柏","水","窦","章","云","苏","潘","葛","奚","范","彭","郎","鲁","韦","昌","马","苗","凤","花","方","任","袁","柳","鲍","史","唐","费","薛","雷","贺","倪","汤","滕","殷","罗","毕","郝","安","常","傅","卞","齐","元","顾","孟","平","黄","穆","萧","尹","姚","邵","湛","汪","祁","毛","狄","米","伏","成","戴","谈","宋","茅","庞","熊","纪","舒","屈","项","祝","董","梁","杜","阮","蓝","闵","季","贾","路","娄","江","童","颜","郭","梅","盛","林","钟","徐","邱","骆","高","夏","蔡","田","樊","胡","凌","霍","虞","万","支","柯","管","卢","莫","柯","房","裘","缪","解","应","宗","丁","宣","邓","单","杭","洪","包","诸","左","石","崔","吉","龚","程","嵇","邢","裴","陆","荣","翁","荀","于","惠","甄","曲","封","储","仲","伊","宁","仇","甘","武","符","刘","景","詹","龙","叶","幸","司","黎","溥","印","怀","蒲","邰","从","索","赖","卓","屠","池","乔","胥","闻","莘","党","翟","谭","贡","劳","逄","姬","申","扶","堵","冉","宰","雍","桑","寿","通","燕","浦","尚","农","温","别","庄","晏","柴","瞿","阎","连","习","容","向","古","易","廖","庾","终","步","都","耿","满","弘","匡","国","文","寇","广","禄","阙","东","欧","利","师","巩","聂","关","荆","司马","上官","欧阳","夏侯","诸葛","闻人","东方","赫连","皇甫","尉迟","公羊","澹台","公冶","宗政","濮阳","淳于","单于","太叔","申屠","公孙","仲孙","轩辕","令狐","徐离","宇文","长孙","慕容","司徒","司空"};
		String arrMing[] = new String[]{"伟","刚","勇","毅","俊","峰","强","军","平","保","东","文","辉","力","明","永","健","世","广","志","义","兴","良","海","山","仁","波","宁","贵","福","生","龙","元","全","国","胜","学","祥","才","发","武","新","利","清","飞","彬","富","顺","信","子","杰","涛","昌","成","康","星","光","天","达","安","岩","中","茂","进","林","有","坚","和","彪","博","诚","先","敬","震","振","壮","会","思","群","豪","心","邦","承","乐","绍","功","松","善","厚","庆","磊","民","友","裕","河","哲","江","超","浩","亮","政","谦","亨","奇","固","之","轮","翰","朗","伯","宏","言","若","鸣","朋","斌","梁","栋","维","启","克","伦","翔","旭","鹏","泽","晨","辰","士","以","建","家","致","树","炎","德","行","时","泰","盛","雄","琛","钧","冠","策","腾","楠","榕","风","航","弘","秀","娟","英","华","慧","巧","美","娜","静","淑","惠","珠","翠","雅","芝","玉","萍","红","娥","玲","芬","芳","燕","彩","春","菊","兰","凤","洁","梅","琳","素","云","莲","真","环","雪","荣","爱","妹","霞","香","月","莺","媛","艳","瑞","凡","佳","嘉","琼","勤","珍","贞","莉","桂","娣","叶","璧","璐","娅","琦","晶","妍","茜","秋","珊","莎","锦","黛","青","倩","婷","姣","婉","娴","瑾","颖","露","瑶","怡","婵","雁","蓓","纨","仪","荷","丹","蓉","眉","君","琴","蕊","薇","菁","梦","岚","苑","婕","馨","瑗","琰","韵","融","园","艺","咏","卿","聪","澜","纯","毓","悦","昭","冰","爽","琬","茗","羽","希","欣","飘","育","滢","馥","筠","柔","竹","霭","凝","晓","欢","霄","枫","芸","菲","寒","伊","亚","宜","可","姬","舒","影","荔","枝","丽","阳","妮","宝","贝","初","程","梵","罡","恒","鸿","桦","骅","剑","娇","纪","宽","苛","灵","玛","媚","琪","晴","容","睿","烁","堂","唯","威","韦","雯","苇","萱","阅","彦","宇","雨","洋","忠","宗","曼","紫","逸","贤","蝶","菡","绿","蓝","儿","翠","烟"};

		int counta = arrXing.length;
		int countb = arrMing.length;
		int len = randomNumber(1,2);
		String str = "";
		str += arrXing[randomNumber(0,counta - 1)];
		for(int i=0;i<len;i++) {
			str += arrMing[randomNumber(0,countb - 1)];
		}
		
		return str;
	}

	/**
	 * 生成一定范围随机数
	 * @param start
	 * @param end
	 * @return
	 */
	public static int randomNumber(int start,int end){
		Random rand = new Random();
		int randNum = rand.nextInt(end-start+1)+start;
		return randNum;
	}
	
	/**生成血型
	 * 
	 */
	public static String randomBloodType() {
		String [] bloodtype = {"A型","B型","AB型","O型","不详"};
		return bloodtype[randomNumber(0, 4)];
	}
	
	/**
	 * 随机日期字符串 "yyyy-MM-dd HH:mm:ss" 随意写
	 * @return
	 */
	public static String randomDateStr(String gs) {
		Random rand = new Random();
		SimpleDateFormat format = new SimpleDateFormat(gs);
		Calendar cal = Calendar.getInstance();
		cal.set(2017, 0, 1);
		long start = cal.getTimeInMillis();
		cal.set(2017, 5, 28);
		long end = cal.getTimeInMillis();
		Date d = new Date(start + (long)(rand.nextDouble() * (end - start)));
		return format.format(d);
	}
	
	/**
	 * 随机日期字符串
	 * @return
	 */
	public static String randomDateTimeStr() {
		Random rand = new Random();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.set(2017, 0, 1);
		long start = cal.getTimeInMillis();
		cal.set(2017, 5, 28);
		long end = cal.getTimeInMillis();
		Date d = new Date(start + (long)(rand.nextDouble() * (end - start)));
		return format.format(d);
	}
	
	/**
	 * "yyyy-MM-dd HH:mm:ss" 随意写
	 * @param "yyyy-MM-dd HH:mm:ss" 随意写
	 * @return
	 */
	public static String nowDateTimeStr(String gs) {
		   Date dt = new Date();     
		   //最后的aa表示“上午”或“下午”    HH表示24小时制    如果换成hh表示12小时制     
		   SimpleDateFormat sdf = new SimpleDateFormat(gs);     
		   return sdf.format(dt);     
	}
	
	/**
	 * "yyyy-MM-dd HH:mm:ss" 随意写
	 * @param gs 格式
	 * @param d 偏差
	 * @return
	 */
	public static String getDateStr(int d) {
		Calendar   cal   =   Calendar.getInstance();
		cal.add(Calendar.DATE, d);
		return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
	}
	
	/**
	 * "yyyy-MM-dd HH:mm:ss" 随意写
	 * @param gs 格式
	 * @param d 偏差
	 * @return
	 */
	public static String getMonthStr(int d) {
		Calendar   cal   =   Calendar.getInstance();
		cal.add(Calendar.MONTH, d);
		return new SimpleDateFormat("yyyy-MM").format(cal.getTime());
	}
	
	public static String getYearStr(int d) {
		Calendar   cal   =   Calendar.getInstance();
		cal.add(Calendar.YEAR, d);
		return new SimpleDateFormat("yyyy").format(cal.getTime());
	}
	
	/**
	 * 随机一个组织机构代码
	 * @return
	 */
	public static String randomOrgCode() {
		String code = "";
		for(int i=0;i<8;i++) {
			code = randomNumber(0, 9)+code;
		}
		int[] ws = { 3, 7, 9, 10, 5, 8, 4, 2 };  
        String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
        int sum = 0;  
        for (int i = 0; i < 8; i++) {  
            sum += str.indexOf(String.valueOf(code.charAt(i))) * ws[i];  
        }  
        int c9 = 11 - (sum % 11);  
        String sc9 = String.valueOf(c9);  
        if (11 == c9) {  
            sc9 = "0";  
        } else if (10 == c9) {  
            sc9 = "X";  
        }  
        return code+"-"+sc9;  
	}
	
	/**
	 * 左右边界取值
	 */
	public static String getParamsByLR(String result, String left, String right) {
		if ("".equals(left) && "".equals(right)) {
			return result;
		}else if ("".equals(left) && result.contains(right)) {
			int x = 0;
			int y = result.indexOf(right);
			//判断是否是左右边界
			if (x<y) {
				return result.substring(x,y);
			}
		}else if ("".equals(right) && result.contains(left)) {
			int y = result.length()-1;
			int x = result.indexOf(left);
			//判断是否是左右边界
			if (x<y) {
				return result.substring(x,y);
			}
		}else if (result.contains(left) && result.contains(right)) {
			int x = result.indexOf(left)+left.length();
			int y = result.indexOf(right, x);
			//判断是否是左右边界
			if (x<y) {
				return result.substring(x,y);
			}
		}
		return null;
	}
	
}
